package com.hudson.broker.monitor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.management.ManagementFactory;

public class ResourceMonitor {
    private static final Logger logger = LoggerFactory.getLogger(ResourceMonitor.class);

    public static void getMemoryResource() {
        // 获取堆内存使用情况
        long heapSize = Runtime.getRuntime().totalMemory();
        long heapMaxSize = Runtime.getRuntime().maxMemory();
        long heapFreeSize = Runtime.getRuntime().freeMemory();

        logger.debug("Heap Size: {}", heapSize / 1024 / 1024 + " MB");
        logger.debug("Max Heap Size: {}", heapMaxSize / 1024 / 1024 + " MB");
        logger.debug("Free Heap Size: {}", heapFreeSize / 1024 / 1024 + " MB");
    }

    public static void cpuUsage() {
        com.sun.management.OperatingSystemMXBean osBean =
                (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

        double cpuUsage = osBean.getCpuLoad();
        logger.debug("CPU Usage: {}", cpuUsage * 100 + "%");
    }

    public static void diskUsage() {
        File file = new File("C:\\JAVA\\code\\hudson-mq\\store");
        long totalSpace = file.getTotalSpace(); // 总空间
        long freeSpace = file.getFreeSpace();   // 剩余空间

        logger.debug("Total Space: {}", totalSpace / 1024 / 1024 / 1024 + " GB");
        logger.debug("Free Space: {}", freeSpace / 1024 / 1024 / 1024 + " GB");

    }

    static {

    }

}
